function findPID() {
 pid=$(ps  | grep "go-iot-gateway" | grep -v grep | awk '{print $1}')
 echo "$pid"
}

function startGateway() {
   time=$(date "+%Y-%m-%d %H:%M:%S")
   createDir
   echo "\n${time} 启动进程\n" >> ${GATEWAY_LOG_DIR}/panic.log
   ${GATEWAY_BIN_DIR}/go-iot-gateway start --home-dir "${GATEWAY_HOME_DIR}" > /dev/null 2>> ${GATEWAY_LOG_DIR}/panic.log &
   echo "${time} 进程启动成功"

#nohup command > nohup.log 2 >&1
}

 # currentTimeTs=$(date -d "$(date "+%Y-%m-%d %H:%M:%S")" +%s)

function createDir() {
  if [ ! -d "${GATEWAY_LOG_DIR}" ]; then
      echo "logs 目录不存在，正在创建..."
      mkdir "${GATEWAY_LOG_DIR}"
  fi
}

manglePath() {
    UNAME=$(uname -s)
    if [ "${UNAME:0:6}" == "CYGWIN" ]; then
        echo `cygpath -w "$1"`
    else
        echo $1
    fi
}


########################################################################################################################
# PATHS AND CONFIG
########################################################################################################################

target="$0"
# For the case, the executable has been directly symlinked, figure out
# the correct bin path by following its symlink up to an upper bound.
# Note: we can't use the readlink utility here if we want to be POSIX
# compatible.
iteration=
while [ -L "$target" ]; do
    if [ "$iteration" -gt 100 ]; then
        echo "Cannot resolve path: You have a cyclic symlink in $target."
        break
    fi
    ls=`ls -ld -- "$target"`
    target=`expr "$ls" : '.* -> \(.*\)$'`
    iteration=$((iteration + 1))
done

# Convert relative path to absolute path and resolve directory symlinks
bin=`dirname "$target"`
SYMLINK_RESOLVED_BIN=`cd "$bin"; pwd -P`


if [ -z "$_GATEWAY_HOME_DETERMINED" ]; then
    GATEWAY_HOME=`dirname "$SYMLINK_RESOLVED_BIN"`
fi
#if [ -z "$GATEWAY_LIB_DIR" ]; then GATEWAY_LIB_DIR=$GATEWAY_HOME/lib; fi
#if [ -z "$GATEWAY_PLUGINS_DIR" ]; then GATEWAY_PLUGINS_DIR=$GATEWAY_HOME/plugins; fi
#if [ -z "$GATEWAY_OPT_DIR" ]; then GATEWAY_OPT_DIR=$GATEWAY_HOME/opt; fi


# These need to be mangled because they are directly passed to java.
# The above lib path is used by the shell script to retrieve jars in a
# directory, so it needs to be unmangled.
GATEWAY_HOME_DIR=`manglePath "$GATEWAY_HOME"`
if [ -z "$GATEWAY_CONF_DIR" ]; then GATEWAY_CONF_DIR=$GATEWAY_HOME_DIR/confg; fi
GATEWAY_BIN_DIR=$GATEWAY_HOME_DIR/bin
GATEWAY_LOG_DIR=$GATEWAY_HOME_DIR/logs
GATEWAY_CONF_FILE="gateway.yaml"
YAML_CONF=${GATEWAY_CONF_DIR}/${GATEWAY_CONF_FILE}

### Exported environment variables ###
export GATEWAY_HOME_DIR
export GATEWAY_CONF_DIR
export GATEWAY_BIN_DIR
export GATEWAY_LOG_DIR
#export GATEWAY_PLUGINS_DIR
# export /lib dir to access it during deployment of the Yarn staging files
#export GATEWAY_LIB_DIR
# export /opt dir to access it for the SQL client
#export GATEWAY_OPT_DIR